package handlers

import (
	"booking-service/internal/models"
	"booking-service/internal/services"
	"encoding/json"
	log "github.com/sirupsen/logrus"
	"net/http"
)

type BookingHandler struct {
	bookingService *services.BookingService
}

func NewBookingHandler(bs *services.BookingService) *BookingHandler {
	return &BookingHandler{bookingService: bs}
}

func (h *BookingHandler) CreateOrder(w http.ResponseWriter, r *http.Request) {
	var newOrder models.Order
	if err := json.NewDecoder(r.Body).Decode(&newOrder); err != nil {
		log.Errorf("Invalid request payload: %v", err)
		http.Error(w, "invalid request payload", http.StatusBadRequest)
		return
	}

	if err := h.bookingService.CreateOrder(newOrder); err != nil {
		log.Errorf("Error creating order: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)
	if err := json.NewEncoder(w).Encode(newOrder); err != nil {
		log.Errorf("Error encoding response: %v", err)
		http.Error(w, "failed to encode response", http.StatusInternalServerError)
		return
	}

	log.Infof("Order created successfully: %+v", newOrder)
}
